#
# Copyright xgliu515@gmail.com
#
# Top Makefile
#

include $(NK_ROOT)/macro.mk

ROOT = $(shell pwd)
ROOTFS = $(ROOT)/rootfs
RAMDISK = $(ROOT)/ramdisk
BASEOS = $(ROOT)/baseos
NKSRC = $(ROOT)/nksrc
NKLOADER = $(ROOT)/bootloader
KERNEL = $(ROOT)/linux

FILELIST := $(KERNEL)/.initramfs.list
INITRD := initramfs
VERSION := netkindle bootloader

.PHONY: all
all: osp baseos nkfm netkindle

.PHONY: osp
osp:
	rm -fr $(RAMDISK)
	cp -r $(ROOTFS) $(RAMDISK)
	$(SUDO) mknod $(RAMDISK)/dev/console c 5 1

.PHONY: baseos
baseos:
	make -C $(BASEOS); make -C $(BASEOS) install

.PHONY: nkfm
nkfm:
	make -C $(NKSRC); make -C $(NKSRC) install

.PHONY: loader
loader:
	make -C $(NKLOADER); make -C $(NKLOADER) install

.PHONY: rmsvn
rmsvn:
	find $(RAMDISK) -name "\.svn" | xargs rm -fr

.PHONY: initramfs
initramfs: rmsvn
	sh $(KERNEL)/scripts/gen_initramfs_list.sh $(RAMDISK) > $(FILELIST)
	$(KERNEL)/usr/gen_init_cpio $(FILELIST) > $(INITRD)
	rm -f $(INITRD).gz
	gzip -9 $(INITRD)
	cat /dev/null > $(FILELIST)

.PHONY: kernel
kernel: rmsvn
	sh $(KERNEL)/scripts/gen_initramfs_list.sh $(RAMDISK) > $(FILELIST)
	make -C $(KERNEL) -j 5
	cat /dev/null > $(FILELIST)

.PHONY: netkindle
netkindle: kernel
	cp -f $(KERNEL)/arch/x86_64/boot/bzImage $(ROOT)/vmlinuz-$@

.PHONY: bootloader
bootloader: osp baseos loader kernel
	cp -f $(KERNEL)/arch/x86_64/boot/bzImage $(ROOT)/vmlinuz-$@


